package com.sduept.nwld.dataserver.controller.fault;

import com.alibaba.fastjson.JSONArray;
import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.bigdata.fault.manager.FaultRecordQueryManager;
import com.sduept.bigdata.fault.report.etl.factory.AnalysisModelFactoryBuilder;
import com.sduept.bigdata.fault.report.etl.model.DeviceActionDetailDTO;
import com.sduept.bigdata.fault.report.etl.model.ProtectionActionAnalysisModel;
import com.sduept.bigdata.fault.report.etl.model.ProtectionActionTimeAnalysisModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.List;

@Named
@ViewScoped
public class ProtectActSvgGraphicController extends ProtectActSvgController{

	@Autowired
	private FaultRecordQueryManager recordQueryManager;

	/**
	 * 
	 */
	private static final long serialVersionUID = -4550119131236241772L;
	private String faultId = null;
	private ProtectionActionAnalysisModel pmodel;
	
	private String actTimeList = null;//时空图数据
	private boolean firstFileDataEffective=false;//标识时序图数据是否取自于录波数据
	private boolean secondFileDataEffective=false;
	
	
	@PostConstruct
	public void init() {
		if(StringUtils.isBlank(faultId)) {
			faultId = getParameter("faultId");
		}
		if (StringUtils.isEmpty(faultId)) {
			return;
		}
		FaultRecord f =  recordQueryManager.findById(faultId);
		if(f == null) {
			return;
		}
		String type = null;
		if ("线路".equals(f.getEtype())) {
			type = AnalysisModelFactoryBuilder.xianlu;
		}else if("母线".equals(f.getEtype())) {
			type = AnalysisModelFactoryBuilder.muxian;
		}else if("变压器".equals(f.getEtype())) {
			type = AnalysisModelFactoryBuilder.bianyaqi;
		}else {
			return;
		}
		try {
			pmodel = AnalysisModelFactoryBuilder.getAnalysisModelFactory(type).makeAnalysisReport(f);//pa.getLineFaultAnalysisReport(deviceId, dateTime);
		} catch (Exception e) {
			e.printStackTrace();
		}
		ProtectionActionTimeAnalysisModel firstAction = pmodel.getFirstActions();
		ProtectionActionTimeAnalysisModel secondAction = pmodel.getSecondActions();
		// 组织时间序列图需要的数据
		if(null != pmodel.getFirstSt()
				&& (isEffective(firstAction.getProtectionActionList())
						||isEffective(firstAction.getBreakerActionList()) ||isEffective(firstAction.getBreakerProtectionActionList()))) {
			//第一个站的录波数据有效，时序图取录波数据
			firstFileDataEffective=true;
		}
		if(null != pmodel.getSecondSt()
				&& (isEffective(secondAction.getProtectionActionList())
						||isEffective(secondAction.getBreakerActionList()) ||isEffective(secondAction.getBreakerProtectionActionList()))) {
			//第一个站的录波数据有效，时序图取录波数据
			secondFileDataEffective=true;
		}
		actTimeList = getActTimeListStr();
	}

	// 每个站先判断录波，没有录波的取动作元件
	private String getActTimeListStr() {
		JSONArray jsonArray = new JSONArray();
		com.alibaba.fastjson.JSONObject firstJso=null;
		com.alibaba.fastjson.JSONObject secondJso=null;
		ProtectionActionTimeAnalysisModel firstAction = pmodel.getFirstActions();
		ProtectionActionTimeAnalysisModel secondAction = pmodel.getSecondActions();
		List<DeviceActionDetailDTO> firstActDetailList = new ArrayList<DeviceActionDetailDTO>();
		if (null != firstAction) {
			firstActDetailList = firstAction.getProtectionActionDetailList();
		}
		if (!firstFileDataEffective) {
			if (firstActDetailList.size() > 0) {
				// 如果没有录波文件的话,取动作元件的时间列表
				firstJso = getActListByComponent(pmodel.getFirstSt().getSubstationName(),firstActDetailList);
			}
		} else {
			// 如果有录波文件
			firstJso = getActTimeListByRecorder(pmodel.getFirstSt().getSubstationName(), firstAction);
		}
		if(null!=firstJso) {
			jsonArray.add(firstJso);
		}
		List<DeviceActionDetailDTO> secondActDetailList = new ArrayList<DeviceActionDetailDTO>();
		if (null != secondAction) {
			secondActDetailList = secondAction.getProtectionActionDetailList();
		}
		if (!secondFileDataEffective) {
			if (secondActDetailList.size() > 0) {
				secondJso = getActListByComponent(pmodel.getSecondSt().getSubstationName(),secondActDetailList);
			}
		} else {
			// 如果有录波文件
			secondJso = getActTimeListByRecorder(pmodel.getSecondSt().getSubstationName(), secondAction);
		}
		if(null!=secondJso) {
			jsonArray.add(secondJso);
		}
		return jsonArray.toJSONString();
	}
	
	
	public String getActTimeList() {
		return actTimeList;
	}

	public void setActTimeList(String actTimeList) {
		this.actTimeList = actTimeList;
	}
	
}
